package group.cyfc.studygo.controller.api;

import group.cyfc.studygo.controller.BaseController;
import group.cyfc.studygo.dto.JsonResult;
import group.cyfc.studygo.service.GatewayService;
import group.cyfc.studygo.vo.GatewayVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 关卡操作服务控制器
 * @author Vonlin
 */
@Controller
@Slf4j
@RequestMapping("/studygo/gateway")
public class GatewayController extends BaseController {

    @Autowired
    private GatewayService gatewayService;

    /**
     * 管理员获得对应类型的所有关卡
     * @param grade
     * @param subject
     * @return
     */
    @ApiOperation(value = "管理员获得对应类型的所有关卡",notes = "根据所选的年级、科目返回包含所有关卡实体的数组")
    @ApiImplicitParams({@ApiImplicitParam(name = "grade",value = "关卡年级"),@ApiImplicitParam(name = "subject",value = "关卡科目")})
    @GetMapping("/getAllGateways")
    public ResponseEntity<JsonResult> getAllGateways(@Param("grade")String grade,@Param("subject")String subject){
        JsonResult result =gatewayService.getAllGateways(grade,subject);
        return new ResponseEntity<>(result,result.getCode());
    }

    /**
     * 普通用户获得可选的对应类型关卡
     * @param username
     * @param grade
     * @param subject
     * @return
     */
    @ApiOperation(value = "普通用户获得可选的对应类型关卡",notes = "根据年级、科目与用户闯关记录，返回一个包含一个或多个关卡的数组")
    @ApiImplicitParams({@ApiImplicitParam(name = "username",value = "用户账号"),@ApiImplicitParam(name = "grade",value = "年级"),@ApiImplicitParam(name = "subject",value = "科目")})
    @GetMapping("/getGateways")
    public ResponseEntity<JsonResult> getGateways(@Param("username")String username,@Param("grade")String grade,@Param("subject")String subject){
        return new ResponseEntity<>(gatewayService.getGateways(username,grade,subject), HttpStatus.OK);
    }

    /**
     * 获得对应类型关卡的数目
     * @param grade
     * @param subject
     * @return
     */
    @ApiOperation(value = "获得对应类型关卡的数目",notes = "根据所选的年级、科目返回关卡数目")
    @ApiImplicitParams({@ApiImplicitParam(name = "grade",value = "关卡年级"),@ApiImplicitParam(name = "subject",value = "关卡科目")})
    @GetMapping("/getNumberOfGatewway")
    public ResponseEntity<JsonResult> getNumberOfGateway(@Param("grade")String grade,@Param("subject")String subject){
        return new ResponseEntity<>(new JsonResult().ok(gatewayService.getNumber(grade,subject)),HttpStatus.OK);
    }

    /**
     * 管理员获得对应类型的所有关卡所绑定的试卷id
     * @param grade
     * @param subject
     * @return
     */
    @ApiOperation(value = "管理员获得对应类型的所有关卡所绑定的试卷id",notes = "根据所选的年级、科目返回试卷id数组")
    @ApiImplicitParams({@ApiImplicitParam(name = "grade",value = "关卡年级"),@ApiImplicitParam(name = "subject",value = "关卡科目")})
    @GetMapping(value = "paperIdList")
    public ResponseEntity<JsonResult> getPaperIdList(@Param("grade")String grade,@Param("subject")String subject){
        return new ResponseEntity<>(new JsonResult().ok(gatewayService.getPaperIds(grade,subject)),HttpStatus.OK);
    }

    /**
     * 根据年级、科目、关卡号获得关卡信息
     * @param grade
     * @param subject
     * @param number
     * @return
     */
    @GetMapping("/getGate")
    @ApiOperation(value = "获得一道关卡",notes = "根据年级、科目、关卡号获得关卡信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "grade",value = "年级"),@ApiImplicitParam(name = "subject",value = "科目"),@ApiImplicitParam(name = "number",value = "关卡号")})
    public ResponseEntity<JsonResult> getGateway(@Param("grade")String grade, @Param("subject")String subject, @Param("number")Integer number){
        JsonResult result = gatewayService.getGateway(grade,subject,number);
        return new ResponseEntity<>(result,result.getCode());
    }

    /**
     * 修改关卡
     * @param gatewayVo
     * @return
     */
    @ApiOperation(value = "修改关卡",notes = "")
    @ApiImplicitParam(name = "gatewayVo",value = "一个Json，包含关卡的雪花id、试卷id")
    @PutMapping(value = "changeGateway")
    public ResponseEntity<JsonResult> changeGateway(@RequestBody GatewayVo gatewayVo){
        JsonResult result = gatewayService.changeGateway(gatewayVo);
        return new ResponseEntity<>(result,result.getCode());
    }

    /**
     * 保存一道关卡
     * @param gatewayVo
     * @return
     */
    @ApiOperation(value = "保存关卡",notes = "")
    @ApiImplicitParam(name = "gatewayVo",value = "一个Json，包含关卡的年级、科目、关卡号、试卷id")
    @PostMapping(value = "saveGateway")
    public ResponseEntity<JsonResult> saveGateway(@RequestBody GatewayVo gatewayVo){
        JsonResult result = gatewayService.saveGateway(gatewayVo);
        return new ResponseEntity<>(result,result.getCode());
    }

    /**
     * 删除一道关卡
     * @param gatewayVo
     * @return
     */
    @ApiOperation(value = "删除关卡",notes = "根据关卡的雪花id删除一道关卡")
    @ApiImplicitParam(name = "gatewayVo",value = "一个Json，包含关卡的雪花id")
    @DeleteMapping(value = "deleteGateway")
    public ResponseEntity<JsonResult> deleteGateway(@RequestBody GatewayVo gatewayVo){
        JsonResult result = gatewayService.deleteGateway(gatewayVo);
        return new ResponseEntity<>(result,result.getCode());
    }
}
